home *** CD-ROM | disk | FTP | other *** search
- #include <fstream.h>
- #include <iomanip.h>
- #include <stdlib.h>
-
- #define DEBUG 1
-
- char *vers = "$VER: MpegAnalys-r 0.2 [16. August 2000]";
-
- int errors=0;
- long firstvalidframe=-1;
-
- class frame
- {
- public:
- long position; // the absolute position of the first headerbyte in file
- short invalidheader; // BOOL, indicates whether this frame is ok
- short framelength; // contains expected length of frame in bytes, calculated from header
- short headercounter;
- // Mpeg Information
- short id;
- short layer;
- short bitrate;
- long frequency;
- short protection; // BOOL
- short priv; // BOOL
- short padding; // BOOL, indicates whether an additional bit is present
- frame()
- {
- invalidheader=0;
- framelength=0;
- id=0;
- layer=0;
- bitrate=0;
- frequency=0;
- protection=0;
- priv=0;
- padding=0;
- // cout <<this<<" created"<<endl;
- }
- ~frame()
- {
- // cout <<this<<" removed"<<endl;
- }
- };
-
- int main(int argc, char **argv)
- {
- fstream ein;
- class frame *thisframeptr=0, *lastframeptr=0, *errorframeptr=0, *lastvalidframeptr=0, *nextvalidframeptr=0;
- short infolevel=0, headercounter=0;
- long filesize=0, header[3]={0,0,0}, checkbits=0, actualposition=0, framingerrors=0, invalidtagcounter=0;
- cout <<"| MpegAnalys-r 0.2 |"<<endl;
- cout <<"| by radiat-r@Sascha-Ploss.de |"<<endl;
- if (argc > 3) cout <<"Wrong parameters\nUsage: "<<argv[0]<<" filename [infolevel]"<<endl;
- if (argc > 2) infolevel=atoi(argv[2]);
- ein.open(argv[1],ios::in);
- if (ein.is_open())
- {
- ein.seekg(0, ios::end); /* move to last byte */
- filesize=ein.tellg(); /* get size of file */
- ein.seekg(0, ios::beg); /* move back to start */
- if (filesize > 1)
- {
- header[0]=ein.get(); /* get first header */
- header[1]=ein.get(); /* get first header */
- header[2]=ein.get(); /* get first header */
- // while ((header[0] != EOF) && (firstvalidframe == -1)) /* loop till eof or first valid header is known */
- while (header[2] != EOF) /* loop till eof */
- {
- /* -------------------- Main Program Loop ------------------------------- */
- checkbits=header[1]/32; /* remove the last 5 bits from char */
- if ((checkbits == 7) && (header[0] == 0xFF)) /* sync found (11 set bits) */
- {
- thisframeptr=new frame;
- if (thisframeptr)
- {
- thisframeptr->position=actualposition;
- thisframeptr->headercounter=headercounter;
- // ------------------ checking frame information ------------------
- checkbits=(header[1]/8)-28; /* get the id bytes */
- thisframeptr->id=4-checkbits; // id == 4 is Mpeg 2.5
- if (thisframeptr->id == 3)
- {
- thisframeptr->invalidheader=1;
- }
- checkbits=(header[1]-((header[1]/8)*8))/2; /* get the layer bytes */
- thisframeptr->layer=4-checkbits;
- if (thisframeptr->layer == 4)
- {
- thisframeptr->invalidheader=1;
- }
- checkbits=header[1]-((header[1]/2)*2); /* get protection bit */
- if (!checkbits)
- {
- thisframeptr->protection=1;
- }
- checkbits=header[2]/16;
- if ((checkbits < 1) || (checkbits > 14))
- {
- thisframeptr->invalidheader=1;
- }
- else
- {
- if (thisframeptr->id == 1)
- {
- if (thisframeptr->layer == 1)
- {
- if (checkbits == 1) thisframeptr->bitrate=32;
- else if (checkbits == 2) thisframeptr->bitrate=64;
- else if (checkbits == 3) thisframeptr->bitrate=96;
- else if (checkbits == 4) thisframeptr->bitrate=128;
- else if (checkbits == 5) thisframeptr->bitrate=160;
- else if (checkbits == 6) thisframeptr->bitrate=192;
- else if (checkbits == 7) thisframeptr->bitrate=224;
- else if (checkbits == 8) thisframeptr->bitrate=256;
- else if (checkbits == 9) thisframeptr->bitrate=288;
- else if (checkbits == 10) thisframeptr->bitrate=320;
- else if (checkbits == 11) thisframeptr->bitrate=352;
- else if (checkbits == 12) thisframeptr->bitrate=384;
- else if (checkbits == 13) thisframeptr->bitrate=416;
- else if (checkbits == 14) thisframeptr->bitrate=448;
- }
- else if (thisframeptr->layer == 2)
- {
- if (checkbits == 1) thisframeptr->bitrate=32;
- else if (checkbits == 2) thisframeptr->bitrate=48;
- else if (checkbits == 3) thisframeptr->bitrate=56;
- else if (checkbits == 4) thisframeptr->bitrate=64;
- else if (checkbits == 5) thisframeptr->bitrate=80;
- else if (checkbits == 6) thisframeptr->bitrate=96;
- else if (checkbits == 7) thisframeptr->bitrate=112;
- else if (checkbits == 8) thisframeptr->bitrate=128;
- else if (checkbits == 9) thisframeptr->bitrate=160;
- else if (checkbits == 10) thisframeptr->bitrate=192;
- else if (checkbits == 11) thisframeptr->bitrate=224;
- else if (checkbits == 12) thisframeptr->bitrate=256;
- else if (checkbits == 13) thisframeptr->bitrate=320;
- else if (checkbits == 14) thisframeptr->bitrate=384;
- }
- else if (thisframeptr->layer == 3)
- {
- if (checkbits == 1) thisframeptr->bitrate=32;
- else if (checkbits == 2) thisframeptr->bitrate=40;
- else if (checkbits == 3) thisframeptr->bitrate=48;
- else if (checkbits == 4) thisframeptr->bitrate=56;
- else if (checkbits == 5) thisframeptr->bitrate=64;
- else if (checkbits == 6) thisframeptr->bitrate=80;
- else if (checkbits == 7) thisframeptr->bitrate=96;
- else if (checkbits == 8) thisframeptr->bitrate=112;
- else if (checkbits == 9) thisframeptr->bitrate=128;
- else if (checkbits == 10) thisframeptr->bitrate=160;
- else if (checkbits == 11) thisframeptr->bitrate=192;
- else if (checkbits == 12) thisframeptr->bitrate=224;
- else if (checkbits == 13) thisframeptr->bitrate=256;
- else if (checkbits == 14) thisframeptr->bitrate=320;
- }
- }
- else /* id == 2 or 2.5 */
- {
- if (thisframeptr->layer == 1)
- {
- if (checkbits == 1) thisframeptr->bitrate=32;
- else if (checkbits == 2) thisframeptr->bitrate=48;
- else if (checkbits == 3) thisframeptr->bitrate=56;
- else if (checkbits == 4) thisframeptr->bitrate=64;
- else if (checkbits == 5) thisframeptr->bitrate=80;
- else if (checkbits == 6) thisframeptr->bitrate=96;
- else if (checkbits == 7) thisframeptr->bitrate=112;
- else if (checkbits == 8) thisframeptr->bitrate=128;
- else if (checkbits == 9) thisframeptr->bitrate=144;
- else if (checkbits == 10) thisframeptr->bitrate=160;
- else if (checkbits == 11) thisframeptr->bitrate=176;
- else if (checkbits == 12) thisframeptr->bitrate=192;
- else if (checkbits == 13) thisframeptr->bitrate=224;
- else if (checkbits == 14) thisframeptr->bitrate=256;
- }
- else if ((thisframeptr->layer == 2) || (thisframeptr->layer == 3)) /* layer 2 or 3 */
- {
- if (checkbits == 1) thisframeptr->bitrate=8;
- else if (checkbits == 2) thisframeptr->bitrate=16;
- else if (checkbits == 3) thisframeptr->bitrate=24;
- else if (checkbits == 4) thisframeptr->bitrate=32;
- else if (checkbits == 5) thisframeptr->bitrate=40;
- else if (checkbits == 6) thisframeptr->bitrate=48;
- else if (checkbits == 7) thisframeptr->bitrate=56;
- else if (checkbits == 8) thisframeptr->bitrate=64;
- else if (checkbits == 9) thisframeptr->bitrate=80;
- else if (checkbits == 10) thisframeptr->bitrate=96;
- else if (checkbits == 11) thisframeptr->bitrate=112;
- else if (checkbits == 12) thisframeptr->bitrate=128;
- else if (checkbits == 13) thisframeptr->bitrate=144;
- else if (checkbits == 14) thisframeptr->bitrate=160;
- }
- }
- }
- checkbits=(header[2]-((header[2]/16)*16))/4; /* get the sampling frequency bits */
- if (checkbits == 3)
- {
- thisframeptr->invalidheader=1;
- }
- else
- {
- if (thisframeptr->id == 1)
- {
- if (checkbits == 0)
- {
- thisframeptr->frequency=44100;
- }
- else if (checkbits == 1)
- {
- thisframeptr->frequency=48000;
- }
- else
- {
- thisframeptr->frequency=32000;
- }
- }
- else if (thisframeptr->id == 2)
- {
- if (checkbits == 0)
- {
- thisframeptr->frequency=22050;
- }
- else if (checkbits == 1)
- {
- thisframeptr->frequency=24000;
- }
- else
- {
- thisframeptr->frequency=16000;
- }
- }
- else if (thisframeptr->id == 4)
- {
- if (checkbits == 0)
- {
- thisframeptr->frequency=11025;
- }
- else if (checkbits == 1)
- {
- thisframeptr->frequency=12000;
- }
- else
- {
- thisframeptr->frequency=8000;
- }
- }
- }
- checkbits=(header[2]-(header[2]/4)*4)/2; /* get padding bit */
- if (checkbits)
- {
- thisframeptr->padding=1;
- }
- checkbits=header[2]-((header[2]/2)*2); /* get private bit */
- if (checkbits)
- {
- thisframeptr->priv=1;
- }
-
- /* add here some code to check the last 8 bit */
-
- if ((thisframeptr->frequency) && (thisframeptr->bitrate))
- {
- if (thisframeptr->layer == 1) /* calculate framelength from header */
- {
- thisframeptr->framelength = (((12*thisframeptr->bitrate*1000)/thisframeptr->frequency)+thisframeptr->padding)*4;
- }
- else
- {
- thisframeptr->framelength = ((144*thisframeptr->bitrate*1000)/thisframeptr->frequency)+thisframeptr->padding;
- }
- }
- else
- {
- thisframeptr->framelength=0;
- }
- /* ----------------- checking complete -------------------------- */
- if (!(thisframeptr->invalidheader)) // header is valid
- {
- // if (DEBUG) cout <<" + "; // validheader
- if (lastframeptr)
- { // this is not the first frame
- if ((thisframeptr->position) == ((lastframeptr->position)+(lastframeptr->framelength)))
- { // framelength is OK
- if (nextvalidframeptr)
- { // there's still a bad frame before
- if (nextvalidframeptr->position == lastframeptr->position)
- {
- cout <<"Frame "<<setw(5)<<lastvalidframeptr->headercounter<<" @ byte "<<setw(8)<<lastvalidframeptr->position<<" is "<<((nextvalidframeptr->position)-(lastvalidframeptr->position))-(lastvalidframeptr->framelength)<<" bytes too large."<<endl;
- }
- else
- {
- cout <<"Frame "<<setw(5)<<lastvalidframeptr->headercounter<<" @ byte "<<setw(8)<<lastvalidframeptr->position<<" is "<<(lastvalidframeptr->position)+(lastvalidframeptr->framelength)-(lastframeptr->position)<<" bytes too short."<<endl;
- }
- framingerrors++;
- delete nextvalidframeptr;
- nextvalidframeptr=0;
- delete lastvalidframeptr;
- lastvalidframeptr=0;
- }
- if (infolevel)
- {
- if (infolevel > 1)
- {
- if (lastframeptr->id == 4)
- {
- cout <<"Frame "<<setw(5)<<lastframeptr->headercounter<<" @ byte "<<setw(8)<<lastframeptr->position<<", Mpeg 2.5, layer "<<lastframeptr->layer<<", "<<setw(3)<<lastframeptr->bitrate<<" Kbps, "<<setw(6)<<lastframeptr->frequency<<" Hz, OK "<<endl;
- }
- else
- {
- cout <<"Frame "<<setw(5)<<lastframeptr->headercounter<<" @ byte "<<setw(8)<<lastframeptr->position<<", Mpeg "<<setw(3)<<lastframeptr->id<<", layer "<<lastframeptr->layer<<", "<<setw(3)<<lastframeptr->bitrate<<" Kbps, "<<setw(6)<<lastframeptr->frequency<<" Hz, OK "<<endl;
- }
- }
- else
- {
- cout <<"Frame "<<setw(5)<<lastframeptr->headercounter<<" @ byte "<<setw(8)<<lastframeptr->position<<" OK "<<endl;
- }
- }
- if (errorframeptr) delete errorframeptr; // clean up
- errorframeptr=0;
- if (firstvalidframe == -1)
- {
- firstvalidframe=lastframeptr->position;
- headercounter++;
- (thisframeptr->headercounter)++;
- }
- delete lastframeptr;
- lastframeptr=thisframeptr;
- thisframeptr=0;
- headercounter++;
- }
- else if ((thisframeptr->position) < ((lastframeptr->position)+(lastframeptr->framelength)))
- { // Header inside frame
- if (!(errorframeptr))
- {
- errorframeptr=thisframeptr;
- thisframeptr=0;
- }
- else
- {
- delete thisframeptr;
- thisframeptr=0;
- }
- }
- else // Header outside frame
- {
- if (errorframeptr) // maybe this is the next valid frame
- {
- if (!(lastvalidframeptr))
- {
- lastvalidframeptr=lastframeptr;
- nextvalidframeptr=thisframeptr;
- }
- else
- {
- delete lastframeptr;
- lastframeptr=0;
- delete thisframeptr;
- thisframeptr=0;
- }
- lastframeptr=errorframeptr;
- errorframeptr=0;
- thisframeptr=0;
- ein.seekg(lastframeptr->position+1);
- actualposition=lastframeptr->position+1;
- header[0]=ein.get();
- header[1]=ein.get();
- header[2]=ein.get();
- }
- else // this is the next valid header
- {
- cout <<"Frame "<<setw(5)<<lastframeptr->headercounter<<" @ byte "<<setw(8)<<lastframeptr->position<<" is "<<thisframeptr->position-lastframeptr->position-lastframeptr->framelength<<" bytes too long."<<endl;
- framingerrors++;
- delete lastframeptr;
- lastframeptr=thisframeptr;
- thisframeptr=0;
- }
- }
- }
- else // this is the first valid header
- {
- // cout <<"First valid header @ byte "<<thisframeptr->position<<endl;
- lastframeptr=thisframeptr;
- thisframeptr=0;
- }
- }
- else // invalid header, forget it
- {
- if (lastframeptr->position+lastframeptr->framelength == thisframeptr->position)
- {
- cout <<"Invalid header at right position (@ byte "<<thisframeptr->position<<")"<<endl;
- }
- else
- {
- // cout <<"Skipping invalid header @ byte "<<thisframeptr->position<<endl;
- }
- delete thisframeptr;
- thisframeptr=0;
- }
- }
- else // new frame
- {
- cout <<"Error, could not allocate memory"<<endl;
- errors++;
- }
- }
- else if ((header[0] == 'T') && (header[1] == 'A') && (header[2] == 'G')) /* tag found */
- {
- if (actualposition+128 == filesize)
- {
- cout <<"Valid TAG found"<<endl;
- }
- else
- {
- cout <<"Invalid TAG @ byte "<<actualposition<<endl;
- invalidtagcounter++;
- }
- }
- header[0]=header[1];
- header[1]=header[2]; /* prepare for next loop */
- header[2]=ein.get();
- actualposition++;
- if (DEBUG && (filesize != ein.tellg()) && (actualposition+3 != ein.tellg())) cout <<"actualposition: "<<actualposition<<", real position: "<<ein.tellg()<<endl;
- } // loop till eof
- }
- else // filesize < 1
- {
- cout <<"Error, inputfile \""<<argv[1]<<"\" is too small."<<endl;
- errors++;
- }
- }
- else // ein.is_open
- {
- cout <<"Error while opening inputfile: "<<argv[1]<<endl;
- errors++;
- }
- if (firstvalidframe != -1)
- {
- cout <<headercounter-1<<" valid frames found."<<endl;
- cout <<"First valid frame starts at byte "<<firstvalidframe<<endl;
- if (framingerrors) cout<<"Errors in frames: "<<framingerrors<<endl;
- if (invalidtagcounter) cout <<"Invalid TAGs: "<<invalidtagcounter<<endl;
- }
- else
- {
- cout <<"File \""<<argv[1]<<"\" is not a valid Mpeg audio file."<<endl;
- errors++;
- }
- if (errors) cout <<errors<<" applicationerrors occoured."<<endl;
- return errors;
- }
-
-
-
-
-
-